/* Project:           EasyPIC LCD Potentiometer
 * Author:            Samuel.B.Atlan@gmail.com
 * History:           2010/06/10: initial release
 * Description:       Simple Display of ADC potentiometer value on the JHD162A LCD
 * Test configuration:
     MCU:             PIC16F887
     Dev.Board:       EasyPIC6
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    LCD JHD162A
     SW:              mikroC PRO for PIC 3.8
 * Notes:
     - J15 on RA2 to use potentiometer
     - Value unknownly goes from 0 to 992 on LCD display...
     - Turn on LCD backlight switch (SW8.7) on the development board.
*/

// Constants & Variables *******************************************************
long potentiometer_value;
char *text = "Hello World :-)";              // Text display for line 1

// LCD module connections ******************************************************
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

void init() {
  ANSEL  = 0;                                // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                              // Disable comparators
  C2ON_bit = 0;

  ADC_Init();

  Lcd_Init();                                // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                       // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                  // Cursor off
}

void main(){
  init();
  Lcd_Out(1,1,text);
  while(1) {                                 // Endless loop
    potentiometer_value = ADC_Read(2);       // Get 10-bit results of AD conversion
    LongToStr(potentiometer_value,text);     // Convert Unsigned Int to Str
    Lcd_Out(2,1,text);                       // Write text in first row
  }
}